package CassroomPractice.SingiyLinkedlist;

import CassroomPractice.DoubleLinkedlist.DoubleList;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class SinglyListTest {

    ListOperation<String> list;

    @Before
    public void setUp() throws Exception {
        list = new DoubleList<>();
    }


    @Test
    public void isEmpty() {
        assertTrue(list.isEmpty());
        list.addFirst("a");
        assertFalse(list.isEmpty());
    }


    @Test
    public void addFirst() {
        String one = "one";
        String two ="two";

        list.addFirst(one);
        assertEquals(one, list.getFirst());
        assertEquals(one, list.getLast());
        assertEquals(one, list.get(0));
        assertEquals(1, list.size());

        list.addFirst(two);
        assertEquals(two, list.getFirst());
        assertEquals(one, list.getLast());
        assertEquals(two, list.get(0));
        assertEquals(one, list.get(1));
        assertEquals(2, list.size());
    }

    @Test
    public void addLast() {
        String one = "one";
        String two = "two";

        list.addLast(one);
        assertEquals(one, list.getFirst());
        assertEquals(one, list.getLast());
        assertEquals(one, list.get(0));
        assertEquals(1, list.size());

        list.addLast(two);
        assertEquals(one, list.getFirst());
        assertEquals(two, list.getLast());
        assertEquals(one, list.get(0));
        assertEquals(two, list.get(1));
        assertEquals(2, list.size());
    }

    @Test
    public void add() {
        String one = "one";
        String two = "two";
        String three = "three";

        list.add(0, one);
        assertEquals(one, list.getFirst());
        assertEquals(one, list.getLast());
        assertEquals(one, list.get(0));
        assertEquals(1, list.size());

        list.add(0, two);
        assertEquals(two, list.getFirst());
        assertEquals(one, list.getLast());
        assertEquals(two, list.get(0));
        assertEquals(one, list.get(1));
        assertEquals(2, list.size());

        // two  three one
        list.add(1, three);
        assertEquals(two, list.getFirst());
        assertEquals(one, list.getLast());
        assertEquals(two, list.get(0));
        assertEquals(three, list.get(1));
        assertEquals(one, list.get(2));
        assertEquals(3, list.size());


    }

    @Test
    public void set() {
        String one = "one";
        String two = "two";
        String three = "three";
        String newItem = "new";

        list.addFirst(one);
        list.addFirst(two);
        list.addFirst(three);

        list.set(0, newItem + one);
        assertEquals(newItem + one, list.get(0));

        list.set(1, newItem + two);
        assertEquals(newItem + two, list.get(1));

        list.set(2, newItem + three);
        assertEquals(newItem + three, list.get(2));

        assertEquals(3, list.size());
    }

    @Test
    public void removeFirst() {
        list.addFirst("one");
        list.addFirst("two");
        assertEquals("two", list.removeFirst());
    }

    @Test
    public void removeLast() {
        list.addFirst("one");
        list.addFirst("two");
        assertEquals("one", list.removeLast());
    }

    @Test
    public void remove() {
        String one = "one";
        String two = "two";
        String three = "three";

        list.addFirst(one);
        list.addFirst(two);
        list.addFirst(three);

        list.remove(1);
        assertEquals(2, list.size());
        assertEquals(three, list.getFirst());
        assertEquals(one, list.getLast());
    }

    @Test
    public void testRemove() {
        String one = "one";
        String two = "two";
        String three = "three";

        list.addFirst(one);
        list.addFirst(two);
        list.addFirst(three);

        assertTrue(list.remove(two));
        assertEquals(2, list.size());
        assertEquals(three, list.getFirst());
        assertEquals(one, list.getLast());

        assertTrue(list.remove(three));
        assertEquals(1, list.size());
        assertEquals(one, list.getFirst());
        assertEquals(one, list.getLast());

        assertFalse(list.remove("notExist"));
        assertEquals(1, list.size());

        assertTrue(list.remove(one));
        assertEquals(0, list.size());
    }
}